package com.yuantong.xyerp.rest.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.yuantong.xyerp.core.api.dto.ContractTransportationDto;
import com.yuantong.xyerp.core.api.form.QueryForm;
import com.yuantong.xyerp.core.api.model.Transportation;
import com.yuantong.xyerp.core.api.service.ITransportationService;
import com.yuantong.xyerp.core.provider.dao.ContractTransportationMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Jianguo.Yin on 2021-08-05.
 */
@Slf4j
@RestController
@RequestMapping("/transportation")
@Api(tags = "运输管理")
public class TransportationController {

    @Autowired
    private ITransportationService transportationService;

    @Resource
    private ContractTransportationMapper contractTransportationMapper;

    /**
     * 导出车辆管理信息
     */
    @ApiOperation(value = "导出车辆管理信息",httpMethod = "POST", produces = "application/octet-stream")
    @PostMapping(value = "/export/VehicleManagementInformation", produces = "application/octet-stream;charset=utf-8")
    public void vehicleManagementInformation(HttpServletResponse response,
                              HttpServletRequest request,
                              @ApiParam("查询参数") @RequestBody QueryForm queryForm) {


        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();

        ExcelExportEntity exportEntity = new ExcelExportEntity("创建时间", "createTime", 40);
        exportEntity.setFormat("yyyy-MM-dd hh:mm");
        ExcelExportEntity exportEntity1 = new ExcelExportEntity("车牌号", "carNo",40);
        ExcelExportEntity exportEntity3 = new ExcelExportEntity("司机姓名", "name",40);
        ExcelExportEntity exportEntity4 = new ExcelExportEntity("司机手机号", "mobile",40);
        ExcelExportEntity exportEntity5 = new ExcelExportEntity("备注" ,"remarks",40);
        ExcelExportEntity exportEntity6 = new ExcelExportEntity("状态", "status",40);
        exportEntity6.setReplace(new String[]{"启用_1", "停用_0"});

        entity.add(exportEntity);
        entity.add(exportEntity1);
        entity.add(exportEntity3);
        entity.add(exportEntity4);
        entity.add(exportEntity5);
        entity.add(exportEntity6);


        List<Transportation> list = transportationService.findByFuzzy(queryForm.getStartTime(), queryForm.getEndTime(), queryForm.getFuzzy(), queryForm.getIds());

        String fileName = "车辆管理信息";
        Workbook workbook = ExcelExportUtil
                .exportExcel(new ExportParams(fileName, fileName, ExcelType.HSSF), entity, list);

        ServletOutputStream outputStream = null;
        try {
            /**用 流的形式 */
            response.setContentType("application/octet-stream");
            //中文乱码
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    /**
     * 导出运单信息
     */
    @ApiOperation(value = "导出运单信息",httpMethod = "POST", produces = "application/octet-stream")
    @PostMapping(value = "/export/transportBillManagement", produces = "application/octet-stream;charset=utf-8")
    public void transportBillManagement(HttpServletResponse response,
                                        HttpServletRequest request,
                                        @ApiParam("查询参数") @RequestBody QueryForm queryForm) {


        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();

        ExcelExportEntity exportEntity = new ExcelExportEntity("业务时间", "businessCreateTime", 40);
        exportEntity.setFormat("yyyy-MM-dd hh:mm");
        ExcelExportEntity exportEntity1 = new ExcelExportEntity("业务编号", "contractNo",40);
        ExcelExportEntity exportEntity3 = new ExcelExportEntity("关联业务单号", "docNo",40);
        ExcelExportEntity exportEntity4 = new ExcelExportEntity("业务类型", "businessType",40);
        exportEntity4.setReplace(new String[]{"采购_B","销售_S"});
        ExcelExportEntity exportEntity5 = new ExcelExportEntity("运输路径" ,"transportPath",40);
        ExcelExportEntity exportEntity6 = new ExcelExportEntity("运输重量（吨）", "actualWeight",40);
        ExcelExportEntity exportEntity7 = new ExcelExportEntity("结算类型", "settlementType",40);
        exportEntity7.setReplace(new String[]{"整车_0","按重量_1"});
        ExcelExportEntity exportEntity8 = new ExcelExportEntity("运输费用（元）", "actualFee",40);
        ExcelExportEntity exportEntity9 = new ExcelExportEntity("结算状态", "status",40);
        exportEntity9.setReplace(new String[]{"未添加_0","已添加_1","已完成_2"});

        entity.add(exportEntity);
        entity.add(exportEntity1);
        entity.add(exportEntity3);
        entity.add(exportEntity4);
        entity.add(exportEntity5);
        entity.add(exportEntity6);
        entity.add(exportEntity7);
        entity.add(exportEntity8);
        entity.add(exportEntity9);


        List<ContractTransportationDto> list = contractTransportationMapper.findByFuzzy(
                queryForm.getBusinessType(), queryForm.getStartTime(), queryForm.getEndTime(), queryForm.getFuzzy(), queryForm.getIds());

        String fileName = "运单信息管理";
        Workbook workbook = ExcelExportUtil
                .exportExcel(new ExportParams(fileName, fileName, ExcelType.HSSF), entity, list);

        ServletOutputStream outputStream = null;
        try {
            /**用 流的形式 */
            response.setContentType("application/octet-stream");
            //中文乱码
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
